<?php
	 include_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoLocation.php';
	
	class LocationBus {
		private $_locationDao;	
			
		public function __construct() {
			$this->_locationDao = new LocationDao();		
		}
		
		public function getLocationList($parentID) {
			$listLocation = $this->_locationDao->getLocationList($parentID);
			return $listLocation;
		}
		
		public function getLocationFromID($locationID) {
			$location = $this->_locationDao->getLocationFromID($locationID);
			if (isset($location)) {
				return $location->getName();
			}
		}
		
		public function getLocationById($locationID) {
			return $this->_locationDao->getLocationFromID($locationID);
		}
		
		public function getLocationByRegion($region) {
			return $this->_locationDao->getLocationByRegion($region);
		}
		
		public function getLocationNearest($latitude, $longitude) {
			if (isset($latitude) && isset($longitude)) {
				return $this->_locationDao->getLocationNearest($latitude, $longitude);
			}
		}
		
		public function getAddressInfoBaseOnDB($points) {
			$arr = array();
			$parent = null;
			$lastRegion = null;
			$type = 0;
			foreach ($points as $point) {
				$location = $this->_locationDao->getLocationByRegionAndParent($point, $lastRegion, $type);
				$parent = null;
				if (isset($location)) {
					$parent = $location->getParent();
					$lastRegion = $location->getLocationID();
				} else {
					unset($lastRegion);
				}
				$oneLocation = array('parent' => $parent, 'id' => isset($lastRegion)?$lastRegion:''); 
				$arr[] = $oneLocation;
				$type++;
			}
			return $arr;
		}
	} 
?>